package com.bwie.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;

//全局的过滤器
//模拟一个token验证的逻辑
@Component
//过滤器顺序
@Order(100)
public class MyGlobalFilter2 implements GlobalFilter{

    @Override
    //过滤器的主方法：增强请求（响应）的方法
    //参数1：交换机，可以从中获取请求、响应等对象
    //参数2：网关过滤器链：
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //1.获取请求或 响应对象
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();


        //api演示
        //请求的路由的id
        String id = request.getId();
        System.out.println("routeid:"+id);
        //请求的路径
        RequestPath path = request.getPath();
        System.out.println("path:"+path.value());


        //有权限，放行，转发请求具体微服务
        //继续向下调用下一个过滤器
        return chain.filter(exchange);
    }

}
